package com.gx.smart.lib.http.api;

import android.app.Application;
import android.content.Context;

import com.gx.smart.lib.http.api.service.AlarmApiService;
import com.gx.smart.lib.http.api.service.AuthService;
import com.gx.smart.lib.http.api.service.CommonService;
import com.gx.smart.lib.http.api.service.KnowledgeApiService;
import com.gx.smart.lib.http.api.service.KnowledgeTypeApiService;
import com.gx.smart.lib.http.api.service.MessageRecordApiService;
import com.gx.smart.lib.http.api.service.PlanTaskApiService;
import com.gx.smart.lib.http.api.service.WorkOrderApiService;
import com.gx.smart.lib_http.BuildConfig;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Description: <RetrofitManager><br>
 * Author:      mxdl<br>
 * Date:        2019/6/22<br>
 * Version:     V1.0.0<br>
 * Update:     <br>
 */
public class RetrofitManager {
    public static RetrofitManager retrofitManager;
    public static Context mContext;
    private Retrofit mRetrofit;
    private OkHttpClient.Builder okHttpBuilder;

    private RetrofitManager() {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Logger.d(message);
            }
        });
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        okHttpBuilder = new OkHttpClient.Builder();
        okHttpBuilder.connectTimeout(20 * 1000, TimeUnit.MILLISECONDS).
                readTimeout(20 * 1000, TimeUnit.MILLISECONDS).
                writeTimeout(20 * 1000, TimeUnit.MILLISECONDS);

        okHttpBuilder.addInterceptor(interceptor);

//        //给client的builder添加了一个socketFactory
//        SSLContext sslContext = SSLContextUtil.getDefaultSLLContext();
//        if (sslContext != null) {
//            SSLSocketFactory socketFactory = sslContext.getSocketFactory();
//            okHttpBuilder.sslSocketFactory(socketFactory, new X509TrustManager() {
//                @Override
//                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//
//                }
//
//                @Override
//                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//
//                }
//
//                @Override
//                public X509Certificate[] getAcceptedIssuers() {
//                    return new X509Certificate[0];
//                }
//            });
//        }
//        okHttpBuilder.hostnameVerifier(SSLContextUtil.HOSTNAME_VERIFIER);
        //创建client
        OkHttpClient okHttpClient = okHttpBuilder.build();
        Logger.d("baseurl="+BuildConfig.BASE_URL);
        mRetrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(BuildConfig.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

    }

    public static void init(Application application) {
        mContext = application;
    }

    public static RetrofitManager getInstance() {
        if (retrofitManager == null) {
            synchronized (RetrofitManager.class) {
                if (retrofitManager == null) {
                    retrofitManager = new RetrofitManager();
                }
            }
        }
        return retrofitManager;
    }

    public CommonService getCommonService() {
        return mRetrofit.create(CommonService.class);
    }

    public AuthService getAuthService() {
        return mRetrofit.create(AuthService.class);
    }


    public MessageRecordApiService getMessageRecordApiService() {
        return mRetrofit.create(MessageRecordApiService.class);
    }

    public AlarmApiService getAlarmApiService() {
        return mRetrofit.create(AlarmApiService.class);
    }

    public KnowledgeTypeApiService getKnowledgeTypeApiService() {
        return mRetrofit.create(KnowledgeTypeApiService.class);
    }

    public KnowledgeApiService getKnowledgeApiService() {
        return mRetrofit.create(KnowledgeApiService.class);
    }

    public WorkOrderApiService getWorkOrderApiService() {
        return mRetrofit.create(WorkOrderApiService.class);
    }

    public PlanTaskApiService getPlanTaskApiService() {
        return mRetrofit.create(PlanTaskApiService.class);
    }

}